import java.util.ArrayList;
import java.util.List;

/**
 * 1736. 替换隐藏数字得到的最晚时间
 * https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/
 */
public class Solutions_1736 {
    public static void main(String[] args) {
        List<String> inputs = new ArrayList<>();

        String time1 = "2?:?0";  // output: "23:50"
        String time2 = "0?:3?";  // output: "09:39"
        String time3 = "1?:22";  // output: "19:22"
        String time4 = "??:??";  // output: "23:59"

        inputs.add(time1);
        inputs.add(time2);
        inputs.add(time3);
        inputs.add(time4);

        for (String input : inputs) {
            String result = maximumTime(input);
            System.out.println(result);
        }
    }

    public static String maximumTime(String time) {
        if (time == null) {
            return "";
        }
        char[] arr = time.toCharArray();
        if (arr[0] == '?') {
            // 第 1 位怎么取？看第 2 位是 ? 还是数字，若是数字，还得看是不是小于 4
            if (arr[1] == '?') {
                arr[0] = '2';
            } else if (arr[1] > '3') {
                arr[0] = '1';
            } else {
                arr[0] = '2';
            }
        }
        if (arr[1] == '?') {
            // 第 2 位怎么取？看第 1 位是 1 还是 2
            if (arr[0] == '2') {
                arr[1] = '3';
            } else {
                arr[1] = '9';
            }
        }

        if (arr[3] == '?') {
            arr[3] = '5';
        }
        if (arr[4] == '?') {
            arr[4] = '9';
        }

        return String.valueOf(arr);
    }
}
